<template>
	<view class="container">
		<!-- 顶部倒计时 -->
		<view class="countdown-box">
			<view class="countdown-title">距结束</view>
			<view class="countdown-time">
				<text class="time-item">{{seckillTime.hours}}</text>
				<text class="time-separator">:</text>
				<text class="time-item">{{seckillTime.minutes}}</text>
				<text class="time-separator">:</text>
				<text class="time-item">{{seckillTime.seconds}}</text>
			</view>
		</view>

		<!-- 秒杀商品列表 -->
		<view class="product-list">
			<view class="product-item" v-for="(item, index) in seckillProducts" :key="index" @click="navigateToDetail(item.id)">
				<image :src="item.logo" mode="aspectFill" class="product-img"></image>
				<view class="product-info">
					<text class="product-name">{{item.name}}</text>
					<view class="price-box">
						<text class="seckill-price">¥{{item.seckillPrice}}</text>
						<text class="original-price">¥{{item.price}}</text>
					</view>
					<view class="progress-box">
						<view class="progress-bar">
							<view class="progress-inner" :style="{width: (item.sold/item.stock*100)+'%'}"></view>
						</view>
						<text class="progress-text">已抢{{item.sold}}件</text>
					</view>
					<view class="stock-info">
						<text class="stock-text">库存{{item.stock}}件</text>
						<button class="buy-btn" @click.stop="handleBuy(item)">立即抢购</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {useData} from "/common/useData";

const seckillTime = ref({
	hours: '00',
	minutes: '00',
	seconds: '00'
})

const {modelData:seckillProducts}=useData("/product/search",{size:50})

// 更新秒杀倒计时
const updateSeckillTime = () => {
	const now = new Date()
	const end = new Date()
	end.setHours(23, 59, 59, 999)

	const diff = end - now
	const hours = Math.floor(diff / (1000 * 60 * 60))
	const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
	const seconds = Math.floor((diff % (1000 * 60)) / 1000)

	seckillTime.value = {
		hours: hours.toString().padStart(2, '0'),
		minutes: minutes.toString().padStart(2, '0'),
		seconds: seconds.toString().padStart(2, '0')
	}
}

// 跳转到商品详情
const navigateToDetail = (id) => {
	uni.navigateTo({
		url: `/pages/detail/view?id=${id}`
	})
}

// 立即抢购
const handleBuy = (item) => {
	if(item.stock <= 0) {
		uni.showToast({
			title: '商品已售罄',
			icon: 'none'
		})
		return
	}

	uni.navigateTo({
		url: `/pages/order/confirm?id=${item.id}&type=seckill`
	})
}

onMounted(() => {
	updateSeckillTime()
	setInterval(updateSeckillTime, 1000)
})
</script>

<style>
.container {
	padding-bottom: 20rpx;
}

/* 倒计时样式 */
.countdown-box {
	background-color: #D02B29;
	padding: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.countdown-title {
	color: #FFFFFF;
	font-size: 28rpx;
	margin-right: 20rpx;
}

.countdown-time {
	display: flex;
	align-items: center;
}

.time-item {
	background-color: #FFFFFF;
	color: #D02B29;
	padding: 6rpx 16rpx;
	border-radius: 4rpx;
	font-size: 28rpx;
	font-weight: bold;
}

.time-separator {
	color: #FFFFFF;
	margin: 0 6rpx;
	font-size: 28rpx;
}

/* 商品列表样式 */
.product-list {
	padding: 20rpx;
}

.product-item {
	display: flex;
	background-color: #FFFFFF;
	padding: 20rpx;
	margin-bottom: 20rpx;
	border-radius: 10rpx;
}

.product-img {
	width: 200rpx;
	height: 200rpx;
	border-radius: 10rpx;
	margin-right: 20rpx;
}

.product-info {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.product-name {
	font-size: 28rpx;
	color: #333;
	line-height: 1.4;
}

.price-box {
	display: flex;
	align-items: baseline;
}

.seckill-price {
	font-size: 36rpx;
	color: #D02B29;
	font-weight: bold;
	margin-right: 10rpx;
}

.original-price {
	font-size: 24rpx;
	color: #999;
	text-decoration: line-through;
}

.progress-box {
	display: flex;
	align-items: center;
}

.progress-bar {
	flex: 1;
	height: 6rpx;
	background-color: #EEEEEE;
	border-radius: 3rpx;
	margin-right: 10rpx;
	overflow: hidden;
}

.progress-inner {
	height: 100%;
	background-color: #D02B29;
	border-radius: 3rpx;
}

.progress-text {
	font-size: 24rpx;
	color: #999;
}

.stock-info {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.stock-text {
	font-size: 24rpx;
	color: #666;
}

.buy-btn {
	background-color: #D02B29;
	color: #FFFFFF;
	font-size: 24rpx;
	padding: 8rpx 30rpx;
	border-radius: 30rpx;
	border: none;
}
</style>
